/*
 *         ***Open Internet Server***
 * Author:  
 *	 M. Tawfiqul Islam
 * 	 University of Dhaka, Bangladesh
 *       contact: tawfiq.shobhon@gmail.com
 */
package openInternetServer;

import Controller.ReInitializer;
import Controller.Recovery;
import Settings.KeyMap;
import Settings.ServerData;
import database.LoadFromDatabase;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import shutdown.ShutDownListener;

public class OpenInternetServerImplementation implements ShutDownListener{

    private static OpenInternetServerImplementation openInternetObj = null;
    private static Logger logger = Logger.getLogger(OpenInternetServerImplementation.class);
    public static OpenInternetServerImplementation openInternetServerImplementation;
    public static String serverIPStr = "145.100.98.58";
    public static boolean debug;
    private Thread t[] = new Thread[100];
    private Thread tU[] = new Thread[100];
    private Thread reinit, recovery;

    public static void main(String[] args) throws Exception {

        PropertyConfigurator.configure("log4j.properties");

        //loading Server information from DB
        if (LoadFromDatabase.getInstance().loadServerSettings()) {
            logger.debug("Successfully Loaded Server Information");
            System.out.println(Settings.ServerData.settings.get(KeyMap.SERVER_VERSION));
            logger.debug("Server Version: " + Settings.ServerData.settings.get(KeyMap.SERVER_VERSION));
        } else {
            logger.fatal("Failed to load Server Information");
        }

        //loading test settings from DB
        if (LoadFromDatabase.getInstance().loadTestSettings()) {
            logger.debug("Successfully Loaded Test Settings. Number of Total Settings: " + ServerData.testSettingsObjects.size());

        } else {
            logger.fatal("Failed to load Test Settings");
        }

        openInternetObj = new OpenInternetServerImplementation();

        //starting controller threads
        openInternetObj.startControllerThreads();       

        shutdown.ShutDownService.getInstance().addShutDownListener(openInternetObj);
        
        System.out.println("ISOC Open Internet Server Started Successfully!\n\n");
        logger.debug("ISOC Open Internet Server Started Successfully!\n\n");

    }

    private void startControllerThreads() {
        logger.debug("Starting Controller threads");
        reinit = new Thread(ReInitializer.getInstance());
        reinit.start();
        recovery = new Thread(Recovery.getInstance());
        recovery.start();
    }

    public static OpenInternetServerImplementation getInstance() {
        if (openInternetObj == null) {
            createOpenInternetObj();
        }
        return openInternetObj;
    }

    private static synchronized void createOpenInternetObj() {
        if (openInternetObj == null) {
            try {
                openInternetObj = new OpenInternetServerImplementation();
            } catch (Exception ex) {
                logger.fatal("exception while creating openInternetServerImplementation", ex);
            }
        }
    }

    @Override
    public void shutDown() {
       
        logger.debug("shutting down all the threads");

        //shutting down other controller threads
        Recovery.getInstance().shutDown();
        ReInitializer.getInstance().shutDown();
        System.exit(0);
    }
}
